import { identity, isEmpty, isNumber, pickBy } from 'lodash';
import request from '@/http/request';
import { IQueryParams } from '@/widgets/table-pagination';
import Qs from 'qs';

export function getList(queryParams: any) {
  let filterParam: any = {
    ...queryParams,
    ...pickBy(queryParams.filter, (value) => {
      return !isEmpty(value);
    }),
    ...pickBy(queryParams.filter, isNumber),
  };
  filterParam.page = filterParam.pageNum - 1;
  filterParam.size = filterParam.pageSize;
  delete filterParam.filter;
  delete filterParam.pageNum;
  delete filterParam.pageSize;

  const param = Qs.stringify(filterParam);
  return request.get(`/user/getPage?${param}`);
}

export function fetchUsers(queryParams: any) {
  return request.get(`/user/getInfo?${new URLSearchParams(queryParams)}`);
}

export function createItem(entity: any) {
  return request.post(`/user/addUser`, { ...entity });
}

export function updateItem(entity: any) {
  return request.post(`/user/updateUser`, entity);
}

export function getRole() {
  return request.get(`/user/getRole`);
}

export function assignRole(entity: any) {
  return request.post(`/user/assignRole`, entity);
}

export function forbiddenUser(entity: any) {
  return request.post(`/user/forbidden`, entity);
}
